using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimeSheetBLL;

namespace TimeSheet.Protected
{
    /// <summary>
    /// Summary description for ProjectReport.
    /// </summary>
    public class ProjectReport : Page
    {
        protected Label lblTitle;
        protected Label lblDateRange;
        protected DataGrid dgBillable;
        protected Label lblTotalHours;

        private void Page_Load(object sender, EventArgs e)
        {
            TimeSheetBLL.Project proj = GetProject();
            lblTitle.Text = "Project Report for <u>" + proj.ProjectClient.Name + " - " +
                            proj.Name + "</u>";

            if (GetShowAll())
            {
                lblTotalHours.Text = "" + proj.GetTotalHours().ToString("F");
                lblDateRange.Text = "All project hours are shown.";
            }
            else
            {
                lblTotalHours.Text = "" +
                                     proj.GetTotalHoursByDateRange(
                                         GetFromDate(), GetToDate()).ToString("F");
                lblDateRange.Text = "From " + GetFromDate().ToShortDateString() + " to " +
                                    GetToDate().ToShortDateString();
            }

            BindAllHours(proj, GetFromDate(), GetToDate(), GetShowAll());
        }

        private void BindAllHours(
            TimeSheetBLL.Project proj, DateTime from, DateTime to, bool showAll)
        {
            DataTable dt = null;
            if (showAll)
            {
                dt = proj.GetAllHours();
            }
            else
            {
                dt = proj.GetChargeableHoursByDateRange(GetFromDate(), GetToDate());
            }
            dgBillable.DataSource = dt;
            dgBillable.DataBind();
        }

        private DateTime GetFromDate()
        {
            return Convert.ToDateTime(Server.UrlDecode(Request["from"]));
        }

        private DateTime GetToDate()
        {
            return Convert.ToDateTime(Server.UrlDecode(Request["to"]));
        }

        private bool GetShowAll()
        {
            return Convert.ToBoolean(Server.UrlDecode(Request["showall"]));
        }

        private TimeSheetBLL.Project GetProject()
        {
            TimeSheetBLL.Project proj = new TimeSheetBLL.Project();
            proj.ID = Convert.ToInt64(Server.UrlDecode(Request["id"]));
            BrokerFactory.Fill(proj);
            return proj;
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}